package com.zhj.test.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * @author zhj
 */
public class NettyByteBuf02 {
    public static void main(String[] args) {
        ByteBuf buf = Unpooled.copiedBuffer("hello,world! --北京", CharsetUtil.UTF_8);
        if (buf.hasArray()) {
            byte[] array = buf.array();

            System.out.println(new String(array, CharsetUtil.UTF_8));
            System.out.println("byteBuf = " + buf);
            System.out.println(buf.arrayOffset());
            System.out.println(buf.readerIndex());
            System.out.println(buf.writerIndex());
            System.out.println(buf.capacity());
            System.out.println(buf.readableBytes());
            System.out.println(buf.writableBytes());

            for (int i = 0; i < buf.readableBytes(); i++) {
                System.out.println((char) buf.getByte(i));
            }
            System.out.println(buf.getCharSequence(4,6,CharsetUtil.UTF_8));
        }

        System.out.println("执行完毕！");
    }
}
